import router from './router'
import store from './store/'
import { IRequest } from './utils/request'
import { routerComponents } from './router'
import Layout from '@/layout/Layout.vue'

router.beforeEach((to, from, next) => {

    // console.log('router.beforeEach')
    // console.log('router.to')
    // console.log(to)
    // console.log('router.from')
    // console.log(from)
    // console.log('router.next')
    // console.log(next)

    next()

})

getMenus();

function getMenus(){
    IRequest({
        url: '/menu/'
    }).then(response => {
        const data = response.data
        
        // const homeMenu = data.find(item => item.display == '1')
        // store.commit('components/addHederTab', {
            //     name: homeMenu.title,
            //     path: homeMenu.path
            // })
            
        loadMenu(data)
        loadRouter(data)
    })
}

function loadMenu(data){
    store.commit('components/setAsideMenus', data)
}

function loadRouter(data){
    const routerLayout = {
        path: '',
        component: Layout,
        redirect: '/home',
        children: []
    }
    
    data.forEach( item => {
        const routerComponent = {
            path: item.path,
            name: item.title,
            component: routerComponents[item.name],
            // meta: {title: item.title, icon: item.icon}
        }
        routerLayout.children.push(routerComponent)

        item.subMenuDtoList.forEach( subItem => {
            const routerSubComponent = {
                path: subItem.path,
                name: subItem.title,
                component: routerComponents[subItem.name]
            }
            routerLayout.children.push(routerSubComponent)
        })
        
    })
    router.addRoute(routerLayout)
}
